<?php
/**
 * 红包游戏
 * User: admin
 * Date: 2017/7/28
 * Time: 10:47
 */

namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class RedbagGameController  extends AdminbaseController {


    /**
     * 游戏列表
     */
    function gamelist(){
       $status=I('status');
        $list = M('red_task')->order('uptime desc')->select();
        foreach ($list as $k=>&$li){
            if($status){
                $map['status'] =$status;
            }
            $task=M('par_task')->where('task_id='.$li['id'])->where($map)->find();
            $li['type'] = $task['status'];
            if(!$task){
                unset($list[$k]);
            }
            if($li['status'] ==0){
                $li['status_txt']='拼福中';
            }else if($li['status'] ==1){
                $li['status_txt']='拼福成功';
            }else{
                $li['status_txt']='拼福失败';
            }
            $userinfo=M("users")->field("user_nicename")->where("id=".$li['uid'])->find();
            $li['user_nicename']=$userinfo['user_nicename'];
        }
        $this->assign('lists',$list);
        $this->assign('status',$status);
        $this->display();
    }

    /**
     * 查看详情
     */
    public function  detaillist(){
        $id=I('id');
        $list =M('par_task')->where('task_id='.$id)->select();
        foreach ($list as &$li){
            if($li['status'] ==1){
                $li['status_txt']='进行中';
            }else if ($li['status'] ==2){
                $li['status_txt']='未拆开';
            }else if ($li['status'] ==3){
                $li['status_txt']='退款中';
            }else if ($li['status'] ==4){
                $li['status_txt']='已拆开';
            }else if ($li['status'] ==5){
                $li['status_txt']='已退款';
            }
            $userinfo=M("users")->field("user_nicename")->where("id=".$li['uid'])->find();
            $li['user_nicename']=$userinfo['user_nicename'];
        }
        $this->assign('lists',$list);
        $this->display();
    }

    /**
     * 编辑
     */
    public function edit(){
        $id=I('id');
        $data= I('');
        $task =M('par_task')->where('id='.$id)->find();
        if(IS_POST){
            $red =M('red_task')->where('id='.$task['task_id'])->find();
            $ratio = M('redratio')->where('id=1')->getField('ratio');
            $already_money = $red['already_money']-$red['already_money']*$ratio/100;
            var_dump($red['already_money']*$ratio/100);exit;
            if( $data['drawmoney'] < $already_money ) {
                $already_money = $already_money - $data['drawmoney'];
                $map['already_money']=$already_money;
                $flag=M('red_task')->where('id='.$task['task_id'])->save($map);
            }else{
                $this->error('金额过大，不能超过'.$already_money);
            }
            $data['drawmoney'] = $data['drawmoney'];
            $data['uptime']=time();
            $flag = M('par_task')->where('id='.$id)->save($data);
            if($flag){
                $this->success('修改成功',U('RedbagGame/detaillist',array('id'=>$task['task_id'])));
            }else{
                $this->error('修改失败');
            }
        }
        $task['readonly']='readonly';
        if($task['status'] ==1){
            $task['status_txt']='进行中';
            $task['readonly']="";
        }else if ($task['status'] ==2){
            $task['status_txt']='未拆开';
        }else if ($task['status'] ==3){
            $task['status_txt']='退款中';
        }else if ($task['status'] ==4){
            $task['status_txt']='已拆开';
        }else if ($task['status'] ==5){
            $task['status_txt']='已退款';
        }
        $userinfo=M("users")->field("user_nicename")->where("id=".$task['uid'])->find();
        $task['user_nicename']=$userinfo['user_nicename'];
        $this->assign('task',$task);
        $this->display();
    }

    /*
     * 红包系统抽成
     */
    function  redtatio(){
        if(IS_POST){
            $ratio=$_POST['ratio'];
            $status=$_POST['status'];
            $id=$_POST['id'];
            $data['ratio'] = $ratio;
            $data['status']=$status;
            $data['uptime']=time();
            $data['returnred_time'] = I('post.returnred_time');
            $flag=M('redratio')->where('id='.$id)->save($data);
            if($flag){
                $this->success('设置成功');
            }else{
                $this->error('设置失败');
            }
        }
        $info = M('redratio')->find();
        $this->assign('info',$info);
        $this->display();
    }


    /**
     * 红包规则
     */
    function  redrule(){
        if(IS_POST) {
           // var_dump($_POST);
            $id=$_POST['id'];
            $data['content']=htmlspecialchars_decode($_POST['post_content']);
            $data['createtime']=time();
            $flag=M('redrule')->where('id='.$id)->save($data);
            if($flag){
                $this->success('设置成功');
            }else{
                $this->error('设置失败');
            }
        }
        $info = M('redrule')->find();
        $this->assign('info',$info);
        $this->display();
    }

}